package com.fau.hm.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.validation.ConstraintViolation;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.fau.hm.entity.MedicalAdmin;
import com.fau.hm.entity.MedicalCity;
import com.fau.hm.entity.MedicalEnterprise;
import com.fau.hm.entity.MedicalEnterpriseExtension;
import com.fau.hm.entity.MedicalEnterpriseOutside;
import com.fau.hm.entity.MedicalEnterpriseQueue;
import com.fau.hm.entity.MedicalProvince;
import com.fau.hm.service.IEnterpriseManagerService;
import com.fau.hm.util.SystemConfig;
import com.fau.hm.util.ValidatorUtil;

@Service
@Transactional(readOnly = true)
public class EnterpriseManagerService extends BaseService implements IEnterpriseManagerService {

	@Override
	public MedicalEnterprise findByEid(String eid) {
		return (MedicalEnterprise) this.iBaseDao.getObject(MedicalEnterprise.class, eid);
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean save(MedicalEnterprise bean) {
		Set<ConstraintViolation<Object>> errorInfo = ValidatorUtil.validateObj(bean);
		if(errorInfo.size() > 0) {
			return false;
		} else {
			return this.iBaseDao.saveObject(bean);
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean saveEdit(MedicalEnterprise bean) {
		MedicalEnterprise enterprise = (MedicalEnterprise) this.iBaseDao.getObject(MedicalEnterprise.class, bean.getId());
		enterprise.setName(bean.getName());
		enterprise.setDescription(bean.getDescription());
		enterprise.setProvinceId(bean.getProvinceId());
		enterprise.setCityId(bean.getCityId());
		if(StringUtils.isNotBlank(bean.getNeedStatus()) && (bean.getNeedStatus().length() > 1)) {
			enterprise.setNeedStatus("3");
		} else if(StringUtils.isNotBlank(bean.getNeedStatus()) && (bean.getNeedStatus().length() == 1)) {
			enterprise.setNeedStatus(bean.getNeedStatus());
		} else {
			enterprise.setNeedStatus("0");
		}
		enterprise.setIsLocked(SystemConfig.ENTERPRISE_OPEN);
		enterprise.setUpdateTime(new Date());
		Set<ConstraintViolation<Object>> errorInfo = ValidatorUtil.validateObj(enterprise);
		if(errorInfo.size() > 0) {
			return false;
		} else {
			return this.iBaseDao.saveObject(enterprise);
		}
	}

	@Override
	public boolean includeEnterprise(String name) {
		MedicalEnterprise enterprise = (MedicalEnterprise) this.iBaseDao.getObjectByHql("from MedicalEnterprise m where m.name = ? ", name);
		if(null != enterprise) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean saveRegister(MedicalEnterprise bean) {
		// 默认公司拥有对账和回款
		bean.setNeedStatus("3");
		Set<ConstraintViolation<Object>> errorInfo = ValidatorUtil.validateObj(bean);
		if(errorInfo.size() > 0) {
			return false;
		} else {
			this.iBaseDao.saveObject(bean);
			return true;
		}
	}

	@SuppressWarnings("all")
	@Override
	public List<MedicalProvince> findProvince() {
		return this.iBaseDao.getBySql("from MedicalProvince m ", null, null);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MedicalCity> findCitysByProId(String provinceId) {
		return this.iBaseDao.getBySql("from MedicalCity m where m.pId = ? ", null, provinceId);
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean saveOutSideNum(MedicalEnterpriseOutside outSide) {
		return this.iBaseDao.saveObject(outSide);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MedicalEnterpriseOutside> findOutsideAll(MedicalAdmin admin) {
		return this.iBaseDao.getBySql("from MedicalEnterpriseOutside a where a.eid=? or a.eid=? ", null, admin.getEnterpriseId(), "0");
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MedicalEnterpriseQueue> findQueueList(MedicalAdmin admin) {
		return this.iBaseDao.getBySql("from MedicalEnterpriseQueue a where a.eid=? or a.eid=? ", null, admin.getEnterpriseId(), "0");
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MedicalEnterpriseExtension> findExtensionList(String eid) {
		List<MedicalEnterpriseExtension> extensionList = this.iBaseDao.getBySql("from MedicalEnterpriseExtension a where a.eid=? or eid=? ", null, eid, "0");
		return extensionList;
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void bindOutside(String outsideIds, String eid) {
		this.iBaseDao.updateHql("update MedicalEnterpriseOutside a set a.eid=? where a.eid=? ", "0", eid);
		String[] outsideId = outsideIds.split(",");
		if(StringUtils.isNotBlank(outsideId[0])) {
			for(String id : outsideId) {
				MedicalEnterpriseOutside bean = (MedicalEnterpriseOutside) this.iBaseDao.getObject(MedicalEnterpriseOutside.class, id);
				bean.setEid(eid);
			}
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void bindQueue(String queueIds, String eid) {
		this.iBaseDao.updateHql("update MedicalEnterpriseQueue a set a.eid=? where a.eid=? ", "0", eid);
		String[] queueId = queueIds.split(",");
		if(StringUtils.isNotBlank(queueId[0])) {
			for(String id : queueId) {
				MedicalEnterpriseQueue bean = (MedicalEnterpriseQueue) this.iBaseDao.getObject(MedicalEnterpriseQueue.class, id);
				bean.setEid(eid);
			}
		}
	}
	
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void bindExtension(String extensionIds, String eid) {
		this.iBaseDao.updateHql("update MedicalEnterpriseExtension a set a.eid=? where a.eid=? ", "0", eid);
		String[] extensionId = extensionIds.split(",");
		if(StringUtils.isNotBlank(extensionId[0])) {
			for(String extension : extensionId) {
				MedicalEnterpriseExtension bean = (MedicalEnterpriseExtension) this.iBaseDao.getObject(MedicalEnterpriseExtension.class, extension);
				bean.setEid(eid);
			}
		}
	}
	
}
